<?php

class Ext_Image_Save_Adapter_Fastdfs
{
    protected $_datas    = array();
    protected $_messages = array();
    
    const RENAME_IMAGE_FAILED = 'renameImageFailed';
    protected $_messageTemplates = array(
        self::RENAME_IMAGE_FAILED => "Rename image failed.",
    );
    protected $_messager;
    
    public function __construct()
    {
        $this->_messager = new Ext_Messager($this->_messageTemplates);
    }

    public function save($file)
    {
        $result = array(
            'code'     => Ext_Image_Save_Result::FAILURE,
            'data'     => '',
            'messages' => array(),
        );
        
        $sourceMd5sum = md5_file($file);
        $image = App_Model_DbTable_Image::getInstance()->getRowByMd5sum($sourceMd5sum);
        if($image){
            $imageId = $image['id'];
            $result['code'] = Ext_Image_Save_Result::SUCCESS;
            $result['data'] = $imageId;
            return new Ext_Image_Save_Result($result['code'], $result['data'], $result['messages']);
        }
        
        
        $imageFastdfs = App_Model_DbTable_Image_Fastdfs::getInstance()->getRowByMd5sum($sourceMd5sum);
        if($imageFastdfs){
            $sourceImageId = $imageFastdfs['id'];
        }else{
            $file_info = Ext_Fastdfs::getInstance()->upload($file);
            $group  = $file_info['group_name'];
            $fileId = $file_info['filename'];
            $row = array(
                'md5sum'     => $sourceMd5sum,
                'group'      => $group,
                'fileId'     => $fileId,
                'createTime' => time(),
            );
            $sourceImageId = App_Model_DbTable_Image_Fastdfs::getInstance()->addNew($row);
        }

        $insertImageRow = array(
            'md5sum' => $sourceMd5sum,
            'source' => $sourceImageId,
        );

        $resizer = new Ext_Image_Save_Resize($file);
        $config = Ext_Config::getInstance()->getConfig('image');
        foreach($config->photo->size as $sizeName=>$size){
            $resizeFile = $resizer->resize($sizeName);
            $resizeMd5sum = md5_file($resizeFile);
            
            if($sourceMd5sum == $resizeMd5sum){
                $resizeImageId = $sourceImageId;
            }else{
                $file_info = Ext_Fastdfs::getInstance()->upload($resizeFile);
                $group  = $file_info['group_name'];
                $fileId = $file_info['filename'];
                $row = array(
                    'md5sum'     => $resizeMd5sum,
                    'group'      => $group,
                    'fileId'     => $fileId,
                    'createTime' => time(),
                );
                $resizeImageId = App_Model_DbTable_Image_Fastdfs::getInstance()->addNew($row);
            }
            
            @unlink($resizeFile);
            
            $insertImageRow[$sizeName] = $resizeImageId;
        }
        $imageId = App_Model_DbTable_Image::getInstance()->addNew($insertImageRow);
        
        @unlink($file);
        
        $result['code'] = Ext_Image_Save_Result::SUCCESS;
        $result['data'] = $imageId;
        return new Ext_Image_Save_Result($result['code'], $result['data'], $result['messages']);
        
    }
    
    
}
